import request from '@/utils/request'

// 标准查询参数类型定义
export interface StandardQueryDTO {
  pageNum: number
  pageSize: number
  standardId?: string
  standardType?: string
  itemName?: string
}

// 标准统计数据类型定义
export interface StandardStats {
  total: number
  enabled: number
}

// 最近活动数据类型定义
export interface RecentActivity {
  title: string
  description: string
  time: string
  type: string
  color: string
  hollow: boolean
}


// 分页查询原材料标准
export const queryRawMaterialStandardsService = (params: StandardQueryDTO) => {
  return request.get('/process-engineer/raw-material-standard', { params })
}

// 分页查询半成品标准
export const querySemiFinishedStandardsService = (params: StandardQueryDTO) => {
  return request.get('/process-engineer/semi-finished-Standards', { params })
}

// 分页查询成品标准
export const queryFinishedStandardsService = (params: StandardQueryDTO) => {
  return request.get('/process-engineer/finished-Standards', { params })
}

// 添加标准
export const addStandardService = (qualityStandard: any) => {
  return request.post('/process-engineer/addStandard', qualityStandard)
}

// 更新标准
export const updateStandardService = (qualityStandard: any) => {
  return request.put('/process-engineer/updateStandard', qualityStandard)
}

// 删除标准
export const deleteStandardService = (params: any) => {
  return request.delete('/process-engineer/deleteStandard', { params : params })
}

// 更新标准状态
export const updateStandardStatusService = (qualityStandard: any) => {
  return request.put('/process-engineer/updateStandardStatus', qualityStandard)
}

// 获取原材料标准统计数据
export const getRawMaterialStatsService = () => {
  return request.get('/process-engineer/raw-material-standard', { 
    params: {
      pageNum: 1,
      pageSize: 1000,  // 获取足够多的记录以计算统计数据
      standardType: 'raw'
    }
  })
}

// 获取半成品标准统计数据
export const getSemiFinishedStatsService = () => {
  return request.get('/process-engineer/semi-finished-Standards', { 
    params: {
      pageNum: 1,
      pageSize: 1000,  // 获取足够多的记录以计算统计数据
      standardType: 'semi'
    }
  })
}

// 获取成品标准统计数据
export const getFinishedStatsService = () => {
  return request.get('/process-engineer/finished-Standards', { 
    params: {
      pageNum: 1,
      pageSize: 1000,  // 获取足够多的记录以计算统计数据
      standardType: 'finished'
    }
  })
}

// 获取最近活动数据
// 由于后端没有直接提供最近活动的 API，我们通过查询最近更新的标准来模拟
export const getRecentActivitiesService = async (): Promise<RecentActivity[]> => {
  try {
    // 获取所有类型的标准数据
    const [rawResponse, semiResponse, finishedResponse] = await Promise.all([
      getRawMaterialStatsService(),
      getSemiFinishedStatsService(),
      getFinishedStatsService()
    ])
    
    // 合并所有标准数据
    const allStandards = [
      ...(rawResponse.data?.records || []).map((item: Record<string, any>) => ({ ...item, standardTypeText: '原材料' })),
      ...(semiResponse.data?.records || []).map((item: Record<string, any>) => ({ ...item, standardTypeText: '半成品' })),
      ...(finishedResponse.data?.records || []).map((item: Record<string, any>) => ({ ...item, standardTypeText: '成品' }))
    ]
    
    // 按更新时间排序，取最近的5条
    const sortedStandards = allStandards
      .sort((a, b) => new Date(b.updateTime).getTime() - new Date(a.updateTime).getTime())
      .slice(0, 5)
    
    // 转换为活动数据格式
    return sortedStandards.map(standard => {
      // 根据标准状态和类型设置不同的活动类型和颜色
      let type = 'info'
      let color = '#909399'
      let hollow = true
      let title = ''
      
      if (standard.createTime === standard.updateTime) {
        // 新增标准
        title = `新增了${standard.standardTypeText}质检标准`
        type = 'success'
        color = '#67c23a'
        hollow = false
      } else if (standard.standardStatus === '已启用') {
        // 启用标准
        title = `启用了${standard.standardTypeText}质检标准`
        type = 'primary'
        color = '#409eff'
        hollow = false
      } else if (standard.standardStatus === '已停用') {
        // 停用标准
        title = `停用了${standard.standardTypeText}质检标准`
        type = 'warning'
        color = '#e6a23c'
        hollow = true
      } else {
        // 更新标准
        title = `更新了${standard.standardTypeText}质检标准`
        type = 'info'
        color = '#909399'
        hollow = true
      }
      
      return {
        title,
        description: `${title}：${standard.standardName}`,
        time: standard.updateTime,
        type,
        color,
        hollow
      }
    })
  } catch (error) {
    console.error('获取最近活动数据失败:', error)
    return []
  }
}